<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>舱位价格管理</title>
  </head>
  <body>
    <div id="position_price_form">
    </div>
    <div id="positionGrid" class="x-form-item">
    </div>
    <script>
            
        Ext.onReady(function(){
            var search_from = "";
            var search_to = "";
            var airportTpl = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{airport_name}({airport_code})', '</div></tpl>');
            var from_airport = new Ext.form.ComboBox({
                store: new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: "/admin/getAllAirportComboBox"
                    }),
                    reader: new Ext.data.JsonReader({
                        root: 'root'
                    }, [{
                            name: 'id'
                        }, {
                            name: 'airport_name'
                        }, {
                            name: 'airport_code'
                        }])
                }),
                fieldLabel: '始发城市',
                name: 'from_airport',
                allowBlank: true,
                blankText: "始发城市必须选择",
                emptyText: '请选择...',
                //displayField: 'airport_name',
                //valueField: 'id',
                typeAhead: false,
                loadingText: '正在查找...',
                //width: 200,
                anchor: '90%',
                //pageSize: 10,
                hideTrigger: false,
                tpl: airportTpl,
                minChars: 0,
                itemSelector: 'div.search-item',
                onSelect: function(record){
                    from_airport.setValue(record.data.airport_name);
                    search_from = record.data.airport_code;
                    if (this.isExpanded()) {
                        this.collapse();
                    }
                }
            });
            var to_airport = new Ext.form.ComboBox({
                store: new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: "/admin/getAllAirportComboBox"
                    }),
                    reader: new Ext.data.JsonReader({
                        root: 'root'
                    }, [{
                            name: 'id'
                        }, {
                            name: 'airport_name'
                        }, {
                            name: 'airport_code'
                        }])
                }),
                fieldLabel: '目的城市',
                name: 'to_airport',
                allowBlank: true,
                blankText: "目的城市必须选择",
                emptyText: '请选择...',
                //displayField: 'airport_name',
                //valueField: 'id',
                typeAhead: false,
                loadingText: '正在查找...',
                //width: 200,
                anchor: '90%',
                //pageSize: 10,
                hideTrigger: false,
                tpl: airportTpl,
                minChars: 0,
                itemSelector: 'div.search-item',
                onSelect: function(record){
                    to_airport.setValue(record.data.airport_name);
                    search_to = record.data.airport_code;
                    if (this.isExpanded()) {
                        this.collapse();
                    }
                }
            });
            var airways = new Ext.form.ComboBox({
                store: new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: '/admin/getInternalAirways'
                    }),
                    reader: new Ext.data.JsonReader({
                        root: 'root',
                        id: 'id'
                    }, ['id', 'airways_code', 'airways_name'])
                }),
                name: 'airways',
                loadingText: '正在加载...',
                displayField: 'airways_name',
                valueField: 'airways_code',
                mode: 'remote',
                triggerAction: 'all',
                hiddenName: 'airways_code',//如果不想提交displayField，则在这儿指定以提交ID的值．
                //hiddenId:'type_id',
                fieldLabel: '航空公司',
                emptyText: '请选择...',
                editable: false,
                allowBlank: true,
                width: 80,
                maxHeight: 150,
                tabIndex: 4
            });
            var specialCheckbox = new Ext.form.Checkbox({
                fieldLabel: '特殊舱位',
                hideLabel: true,
                name: 'special_position',
                boxLabel: '特殊舱位',
                checked: false,
                anchor: '99%',
                value: 'yes'
            });
                
            //提交按钮
            var submitButton = new Ext.Button({
                text: '查询',
                handler: function(){
                    if (formPanel.form.isValid()) {
                        var special_checkbox = "";
                        if (specialCheckbox.checked) {
                            special_checkbox = "yes";
                        }
                        var airways_code = airways.getValue();
                        //alert(search_from+"--"+search_to+"--"+special_checkbox+"--"+airways_code+"--");
                        if (from_airport.getValue()==""){
                            search_from = "";
                        }
                        if (to_airport.getValue()==""){
                            search_to = "";
                        }
                        positionDS.baseParams.fromto = search_from + search_to;
                        positionDS.baseParams.airways_code = airways_code;
                        positionDS.baseParams.special = special_checkbox;
                        positionDS.reload();
                    }
                    else {
                        Ext.MessageBox.alert('提示信息', '请将必填项信息录入后再提交.');
                    }
                }
            })
                
            var formPanel = new Ext.form.FormPanel({
                //baseCls: 'x-plain',
                labelWidth: 65,
                width: '100%',
                frame: true,
                labelAlign: 'right',
                items: [{
                        layout: 'column',
                        border: false,
                        items: [{
                                columnWidth: .25,
                                layout: 'form',
                                border: false,
                                items: [from_airport]
                            }, {
                                columnWidth: .25,
                                layout: 'form',
                                border: false,
                                items: [to_airport]
                            }, {
                                columnWidth: .2,
                                layout: 'form',
                                border: false,
                                items: [airways]
                            }, {
                                columnWidth: .15,
                                layout: 'form',
                                border: false,
                                items: [specialCheckbox]
                            }, {
                                columnWidth: .15,
                                layout: 'form',
                                border: false,
                                items: [submitButton]
                            }]
                    }]
            });
                
            formPanel.render('position_price_form');
                
                
            var positionCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                    header: '编号',
                    dataIndex: 'id',
                    hidden: true
                }, {
                    header: '航空公司代码',
                    dataIndex: 'airways_code',
                    sortable: true,
                    width: 70
                }, {
                    header: '航班号',
                    dataIndex: 'airNo',
                    sortable: true,
                    width: 70
                }, {
                    header: '城市对',
                    dataIndex: 'fromto',
                    sortable: true,
                    width: 70
                }, {
                    header: '舱位代码',
                    dataIndex: 'position',
                    sortable: true,
                    width: 100
                }, {
                    header: '价格',
                    dataIndex: 'price',
                    sortable: true,
                    width: 70
                }, {
                    header: '特价',
                    dataIndex: 'special',
                    width: 70
                }, {
                    header: '开始日期',
                    dataIndex: 'start_date',
                    width: 120
                }, {
                    header: '截止日期',
                    dataIndex: 'end_date',
                    width: 120
                }, {
                    header: '退改签规定',
                    dataIndex: 'policy',
                    width: 200
                }]);
                
            var positionDS = new Ext.data.Store({
                proxy: new Ext.data.HttpProxy({
                    url: '/admin/getAllPositionPrice'
                }),
                reader: new Ext.data.JsonReader({
                    root: 'root'
                }, [{
                        name: 'id'
                    }, {
                        name: 'airways_code'
                    }, {
                        name: 'airNo'
                    }, {
                        name: 'fromto'
                    }, {
                        name: 'position'
                    }, {
                        name: 'price'
                    }, {
                        name: 'special'
                    }, {
                        name: 'start_date'
                    }, {
                        name: 'end_date'
                    }, {
                        name: 'policy'
                    }])
            });
                
            var menubar = [{
                    text: '增加舱位价格',
                    tooltip: '增加舱位价格',
                    iconCls: 'addItem',
                    handler: function(){
                        CreatePosition(positionDS);
                    }
                }, '-', {
                    text: '删除舱位价格',
                    tooltip: '删除舱位价格',
                    iconCls: 'remove',
                    handler: function(){
                        DeletePosition(positionDS, positionGridPanel);
                    }
                }, '-', {
                    text: '编辑政策',
                    tooltip: '编辑政策',
                    iconCls: 'editItem',
                    handler: function(){
                        EditPolicy(positionDS, positionGridPanel);
                    }
                }];
                
            var positionGridPanel = new Ext.grid.GridPanel({
                el: 'positionGrid',
                ds: positionDS,
                cm: positionCM,
                loadMask: true,
                autoWidth: true,
                height: 580,
                collapsible: true,
                animCollapse: false,
                title: '舱位价格列表:',
                tbar: menubar
            });
            positionGridPanel.render();
            positionDS.load();
                
            function CreatePosition(dataStore){
                var airways_name = new Ext.form.ComboBox({
                    store: new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: '/admin/getInternalAirways'
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root',
                            id: 'id'
                        }, ['id', 'airways_code', 'airways_name'])
                    }),
                    name: 'airways_name',
                    loadingText: '正在加载...',
                    displayField: 'airways_name',
                    valueField: 'airways_code',
                    mode: 'remote',
                    triggerAction: 'all',
                    hiddenName: 'airways_code',
                    fieldLabel: '航空公司',
                    emptyText: '请选择...',
                    editable: false,
                    allowBlank: false,
                    width: 100,
                    maxHeight: 150,
                    tabIndex: 4
                });
                var air_no = new Ext.form.NumberField({
                    fieldLabel: '航班号',
                    allowBlank: false,
                    blankText: "航班号必须填写数字",
                    name: 'air_no',
                    anchor: '90%'
                });
                var position = new Ext.form.TextField({
                    fieldLabel: '舱位代码',
                    allowBlank: false,
                    blankText: "舱位代码必须填写",
                    name: 'position',
                    anchor: '90%'
                });
                var airportTpl = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{airport_name}({airport_code})', '</div></tpl>');
                var from_port = new Ext.form.ComboBox({
                    store: new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: "/admin/getAllAirportComboBox"
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root'
                        }, [{
                                name: 'id'
                            }, {
                                name: 'airport_name'
                            }, {
                                name: 'airport_code'
                            }])
                    }),
                    fieldLabel: '始发地',
                    name: 'from_port',
                    //hiddenName: 'from_id',
                    allowBlank: false,
                    blankText: "始发地必须选择",
                    //displayField: 'airport_name',
                    //valueField: 'id',
                    typeAhead: false,
                    loadingText: '正在查找...',
                    //width: 230,
                    anchor: '90%',
                    //pageSize: 10,
                    hideTrigger: false,
                    tpl: airportTpl,
                    minChars: 0,
                    itemSelector: 'div.search-item',
                    onSelect: function(record){
                        from_port.setValue(record.data.airport_name);
                        from_code.setValue(record.data.airport_code);
                        if (this.isExpanded()) {
                            this.collapse();
                        }
                    }
                });
                var to_port = new Ext.form.ComboBox({
                    store: new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: "/admin/getAllAirportComboBox"
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root'
                        }, [{
                                name: 'id'
                            }, {
                                name: 'airport_name'
                            }, {
                                name: 'airport_code'
                            }])
                    }),
                    fieldLabel: '目的地',
                    name: 'to_port',
                    //hiddenName: 'to_id',
                    allowBlank: false,
                    blankText: "目的地必须选择",
                    displayField: 'airport_name',
                    valueField: 'id',
                    typeAhead: false,
                    loadingText: '正在查找...',
                    //width: 230,
                    anchor: '90%',
                    //pageSize: 10,
                    hideTrigger: false,
                    tpl: airportTpl,
                    minChars: 0,
                    itemSelector: 'div.search-item',
                    onSelect: function(record){
                        to_port.setValue(record.data.airport_name);
                        to_code.setValue(record.data.airport_code);
                        if (this.isExpanded()) {
                            this.collapse();
                        }
                    }
                });
                var from_code = new Ext.form.Hidden({
                    name: 'from',
                    value: ''
                });
                var to_code = new Ext.form.Hidden({
                    name: 'to',
                    value: ''
                });
                var price = new Ext.form.TextField({
                    fieldLabel: '价格',
                    allowBlank: true,
                    name: 'price',
                    anchor: '90%'
                });
                var start_date = new Ext.form.DateField({
                    fieldLabel: '生效日期',
                    name: 'start_date',
                    id: 'start_date',
                    altFormats: 'Y-m-d',
                    format: 'Y-m-d', //格式化日期
                    //disabledDays: [0,6],	//不让选择周六，周日
                    //disabledDaysText: "周末要休息",
                    invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
                    allowBlank: true,
                    //emptyText: '请选择...',
                    inputType: '2008-01-01',
                    minValue: today,
                    vtype: 'daterange',
                    endDateField: 'end_date',
                    //anchor: '50%'
                    width: 100
                });
                var end_date = new Ext.form.DateField({
                    fieldLabel: '失效日期',
                    name: 'end_date',
                    id: 'end_date',
                    altFormats: 'Y-m-d',
                    format: 'Y-m-d', //格式化日期
                    //disabledDays: [0,6],	//不让选择周六，周日
                    //disabledDaysText: "周末要休息",
                    invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
                    allowBlank: true,
                    //emptyText: '请选择...',
                    inputType: '2008-01-01',
                    minValue: today,
                    vtype: 'daterange',
                    startDateField: 'start_date',
                    //anchor: '50%'
                    width: 100
                });
                var policy = new Ext.form.TextArea({
                    fieldLabel: '退改签',
                    allowBlank: true,
                    name: 'policy',
                    height: 40,
                    anchor: '90%'
                });
                    
                var addFormPanel = new Ext.form.FormPanel({
                    baseCls: 'x-plain',
                    labelWidth: 55,
                    labelAlign: 'right',
                    url: '/admin/addPositionPrice',
                    defaultType: 'textfield',
                    items: [airways_name, air_no, from_port, to_port, from_code, to_code, position, price, {
                            xtype: 'fieldset',
                            checkboxToggle: true,
                            //collapsible: true,
                            checkboxName: 'checkbox_special',
                            title: '特殊舱位',
                            autoHeight: true,
                            //defaults: {width: 210},
                            defaultType: 'textfield',
                            collapsed: false,
                            items: [start_date, end_date]
                        }, policy]
                });
                    
                // define window and show it in desktop
                var window = new Ext.Window({
                    title: '增加舱位价格',
                    width: 300,
                    height: 380,
                    //minWidth: 300,
                    //minHeight: 320,
                    layout: 'fit',
                    plain: true,
                    bodyStyle: 'padding:5px;',
                    buttonAlign: 'center',
                    items: addFormPanel,
                    modal: true,
                    buttons: [{
                            text: '增加',
                            handler: function(){
                                // check form value 
                                if (addFormPanel.form.isValid()) {
                                    if (from_code.getValue() == "") {
                                        Ext.MessageBox.alert('提示', '请选择始发地');
                                    }
                                    else if (to_code.getValue() == "") {
                                        Ext.MessageBox.alert('提示', '请选择目的地');
                                    }
                                    else {
                                        addFormPanel.form.submit({
                                            waitMsg: 'In processing',
                                            success: function(form, action){
                                                Ext.MessageBox.alert('提示', '提交成功');
                                                window.destroy();
                                                //window.hide();
                                                positionDS.reload();
                                            },
                                            failure: function(form, action){
                                                Ext.MessageBox.alert('提交错误消息', '提交失败');
                                            }
                                        });
                                    }
                                }
                                else {
                                    Ext.MessageBox.alert('提示', '请按要求填写必输选项.');
                                }
                            }
                        }, {
                            text: '取消',
                            handler: function(){
                                window.destroy();
                                //window.hide();
                            }
                        }]
                });
                    
                window.show();
            };
                
            function DeletePosition(dataStore, gridPanel){
                // get the selected items
                var m = gridPanel.getSelections();
                if (m.length > 0) {
                    Ext.MessageBox.confirm('提示', '你真的要删除这条记录吗？', function(btn){
                        if (btn == 'yes') {
                            var jsonData = "[";
                            for (var i = 0; i < m.length; i++) {
                                var ss = "{\"id\":\"" + m[i].get("id") + "\"}";
                                if (i == 0) {
                                    jsonData = jsonData + ss;
                                }
                                else {
                                    jsonData = jsonData + "," + ss;
                                }
                                dataStore.remove(m[i]);
                            }
                            jsonData = jsonData + "]";
                            //Ext.MessageBox.alert(jsonData);
                            dataStore.load({
                                params: {
                                    delData: jsonData
                                }
                            });
                        }
                    });
                }
                else {
                    Ext.MessageBox.alert('错误提示', '请先选择要删除的记录');
                }
            };
                
            function EditPolicy(dataStore, gridPanel){
                var m = gridPanel.getSelections();
                if (m.length > 0) {
                    var selectedId = "";
                    for (var i = 0; i < m.length; i++) {
                        if (selectedId!=""){
                            selectedId << ",";
                        }
                        selectedId << m[i].get("id");
                    }
                        
                    var policy = new Ext.form.TextArea({
                        fieldLabel: '退改签',
                        allowBlank: false,
                        name: 'policy',
                        height: 60,
                        anchor: '90%'
                    });
                        
                    var editFormPanel = new Ext.form.FormPanel({
                        baseCls: 'x-plain',
                        labelWidth: 55,
                        labelAlign: 'right',
                        url: '/admin/updatePositionPolicy?ids='+selectedId,
                        defaultType: 'textfield',
                        items: [policy]
                    });
                        
                    var editWin = new Ext.Window({
                        title: '编辑退改签规定',
                        width: 300,
                        height: 150,
                        layout: 'fit',
                        plain: true,
                        bodyStyle: 'padding:5px;',
                        buttonAlign: 'center',
                        items: editFormPanel,
                        modal: true,
                        buttons: [{
                                text: '提交',
                                handler: function(){
                                    if (editFormPanel.form.isValid()) {
                                        editFormPanel.form.submit({
                                            waitMsg: 'In processing',
                                            success: function(form, action){
                                                Ext.MessageBox.alert('提示', '提交成功');
                                                editWin.destroy();
                                                //editWin.hide();
                                                positionDS.reload();
                                            },
                                            failure: function(form, action){
                                                Ext.MessageBox.alert('提交错误消息', '提交失败');
                                            }
                                        })
                                    }
                                }
                            }, {
                                text: '取消',
                                handler: function(){
                                    editWin.destroy();
                                    //editWin.hide();
                                }
                            }]
                    });
                        
                    editWin.show();
                        
                }
                else {
                    Ext.MessageBox.alert('提示', "请先选择一条要编辑记录");
                }
            }
                
                
        });
    </script>
  </body>
</html>
